package com.hedl.elasticsearch.config;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ProjectName: stuy-elasticsearch
 * DateTime: 2023/9/9 20:19
 * Author: Big Watermelon
 * Description:
 */
@Configuration
public class ElasticsearchConfig {

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private Integer port;

    @Value("${elasticsearch.userName}")
    private String userName;

    @Value("${elasticsearch.password}")
    private String password;

    @Value("${elasticsearch.apiKeys}")
    private String apiKeys;

    /**
     * 同步连接
     * @return elasticsearchClient
     */
    @Bean
    public ElasticsearchClient elasticsearchClient(){
        ElasticsearchTransport transport;
        RestClient restClient;
        ElasticsearchClient elasticsearchClient;

        //验证证书类似于密钥
        if (StringUtils.isNotBlank(apiKeys)){
            //apiKey验证模式
            restClient = RestClient
                    .builder(new HttpHost(host,port))
                    .setDefaultHeaders(new Header[]{
                            new BasicHeader("Authorization", "ApiKey " + apiKeys)
                    })
                    .build();
        }else{
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            // 账号密码验证模式
            credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(userName,password));
            restClient = RestClient.builder(new HttpHost(host,port))
                    .setHttpClientConfigCallback(
                            httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
                    .build();
        }

        transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
        elasticsearchClient = new ElasticsearchClient(transport);
        System.out.println("es初始化完成");

        return elasticsearchClient;
    }

    /**
     * 异步连接
     * @return asyncClient
     */
    @Bean
    public ElasticsearchAsyncClient elasticsearchAsyncClient(){
        ElasticsearchTransport transport;
        RestClient restClient;
        ElasticsearchAsyncClient asyncClient;

        if (StringUtils.isNotBlank(apiKeys)){
            //apiKey验证模式
            restClient = RestClient
                    .builder(new HttpHost(host,port))
                    .setDefaultHeaders(new Header[]{
                            new BasicHeader("Authorization", "ApiKey " + apiKeys)
                    })
                    .build();
        }else{
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            // 账号密码验证模式
            credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(userName,password));
            restClient = RestClient.builder(new HttpHost(host,port))
                    .setHttpClientConfigCallback(
                            httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
                    .build();
        }

        transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
        asyncClient = new ElasticsearchAsyncClient(transport);
        System.out.println("es初始化完成");

        return asyncClient;
    }
}
